package com.kancy.publicapi.web.controller;

import com.kancy.publicapi.common.util.BankCardUtil;
import com.kancy.publicapi.common.util.EmailUtil;
import com.kancy.publicapi.common.util.IDCardUtil;
import com.kancy.publicapi.common.util.NameUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

/**
 * <p>
 * RandomController
 * </p>
 *
 * @author: kancy
 * @date: 2019/11/29 14:45
 **/
@RestController
public class RandomController {
    private char[] chars = "qwertyuiopasdfghjklzxcvbnmQAZWSXEDCRFVTGBYHNUJMIKLOP1234567890".toCharArray();

    /**
     * uuid
     * @return
     */
    @RequestMapping(value = "/random/uuid", method = {RequestMethod.GET, RequestMethod.POST})
    public String uuid(){
        return UUID.randomUUID().toString();
    }

    /**
     * 随机数字
     * @param min
     * @param max
     * @return
     */
    @RequestMapping(value = "/random/number", method = {RequestMethod.GET, RequestMethod.POST})
    public String number(@RequestParam(value = "min", required = false, defaultValue = "0") Long min,
                         @RequestParam(value = "max", required = false, defaultValue = "10") Long max){
        return String.valueOf(ThreadLocalRandom.current().nextLong(min, max));
    }

    /**
     * 随机
     * @param len
     * @return
     */
    @RequestMapping(value = "/random/string", method = {RequestMethod.GET, RequestMethod.POST})
    public String string(@RequestParam(value = "len", required = false, defaultValue = "3") Integer len){
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < len; i++) {
            buffer.append(chars[ThreadLocalRandom.current().nextInt(0, chars.length)]);
        }
        return buffer.toString();
    }

    /**
     * 随机邮件
     * @param domain
     * @return
     */
    @RequestMapping(value = "/random/email", method = {RequestMethod.GET, RequestMethod.POST})
    public String email(@RequestParam(value = "domain", required = false) String domain){
        if (StringUtils.isEmpty(domain)){
            return EmailUtil.create();
        }
        return EmailUtil.create(domain);
    }
    /**
     * 随机名称
     * @param len
     * @return
     */
    @RequestMapping(value = "/random/name", method = {RequestMethod.GET, RequestMethod.POST})
    public String name(@RequestParam(value = "len", required = false, defaultValue = "3") Integer len){
        return NameUtil.fullName(len);
    }
    /**
     * 随机名称
     * @param districtcode
     * @param birthday
     * @return
     */
    @RequestMapping(value = "/random/idcard", method = {RequestMethod.GET, RequestMethod.POST})
    public String idcard(
            @RequestParam(value = "districtcode", required = false) String districtcode,
            @RequestParam(value = "birthday", required = false) String birthday){
        if (Objects.nonNull(districtcode) && districtcode.trim().length() != 6){
            return "districtcode len must be equals 6";
        }
        if (Objects.nonNull(birthday)){
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            format.setLenient(false);
            try {
                format.parse(birthday);
            } catch (ParseException e) {
                return "birthday format must be yyyyMMdd";
            }
        }
        if (Objects.isNull(districtcode) && !Objects.isNull(birthday)){
            return IDCardUtil.create(IDCardUtil.getdistrictcode(), birthday);
        }
        if (!Objects.isNull(districtcode) && Objects.isNull(birthday)){
            return IDCardUtil.create(districtcode);
        }
        return IDCardUtil.create();
    }

    /**
     * 身份证校验
     * @param no
     * @return
     */
    @RequestMapping(value = "/random/idcardcheck/{no}", method = {RequestMethod.GET, RequestMethod.POST})
    public boolean idcardcheck(@PathVariable("no") String no){
        return IDCardUtil.isPersonId(no);
    }

    /**
     * 随机ip
     * @return
     */
    @RequestMapping(value = "/random/ip", method = {RequestMethod.GET, RequestMethod.POST})
    public String ip(){
        StringBuffer sb = new StringBuffer();
        sb.append(ThreadLocalRandom.current().nextInt(10,200)).append(".");
        sb.append(ThreadLocalRandom.current().nextInt(0,255)).append(".");
        sb.append(ThreadLocalRandom.current().nextInt(0,255)).append(".");
        sb.append(ThreadLocalRandom.current().nextInt(1,255));
        return sb.toString();
    }

    /**
     * 随机银行卡号
     * @return
     */
    @RequestMapping(value = "/random/bankcard", method = {RequestMethod.GET, RequestMethod.POST})
    public String bankcard(@RequestParam(value = "len", required = false, defaultValue = "19") Integer len){
        return BankCardUtil.create(len);
    }

    /**
     * 随机银行卡名称
     * @return
     */
    @RequestMapping(value = "/random/bankname", method = {RequestMethod.GET, RequestMethod.POST})
    public String bankname(@RequestParam String no){
        if (StringUtils.isEmpty(no)){
            return "银行卡号必须传：param name is no";
        }
        return BankCardUtil.getBankName(no);
    }

}
